package com.pengpeng.server.dao;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.springframework.jdbc.core.JdbcTemplate;

import com.pengpeng.server.database.Database;
import com.pengpeng.server.database.DatabaseFactory;
import com.pengpeng.server.database.ISqlObj;
import com.pengpeng.server.entity.PengPengMessage;

public class MessageDao{

	private DatabaseFactory databasefactory;
	public boolean updateConfirmtime(PengPengMessage ppmessage,Date confirmtime)
	{
		Database database=databasefactory.getDatabase("default");
		String sqlstr="";
		JdbcTemplate jdbctemplate=database.getJdbcTemplate();
		
		
		
		SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String confirmtimestr=dateformat.format(confirmtime);
		
		
		Object[] params = new Object[] {confirmtimestr,ppmessage.getUserphonenum(),ppmessage.getMessageid()};
		ISqlObj sqlobj=database.getSqlobj();
		sqlstr=sqlobj.getSql("updateMessage");
		jdbctemplate.update(sqlstr,params);		
		return true;
	}
	
	public boolean insert(PengPengMessage ppmessage)
	{
		Database database=databasefactory.getDatabase("default");
		String sqlstr="";
		JdbcTemplate jdbctemplate=database.getJdbcTemplate();
		
		String userphonenum=ppmessage.getUserphonenum();
		String messageid=ppmessage.getMessageid();
		String messagetype=ppmessage.getMessagetype();
		String messagetext=ppmessage.getMessagetext();
		String target=ppmessage.getTarget();
		String ifneedconfirm=ppmessage.getIfneedconfirm();
		String ifconfirmed="0";
		
		SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String messagetimestr=dateformat.format(ppmessage.getMessagetime());
		
		String receivetimestr=dateformat.format(new Date());
		
		
		Object[] params = new Object[] {userphonenum,messageid,messagetype,messagetext,target,ifneedconfirm,ifconfirmed,messagetimestr,receivetimestr};
		ISqlObj sqlobj=database.getSqlobj();
		sqlstr=sqlobj.getSql("insertMessage");
		jdbctemplate.update(sqlstr,params);		
		return true;
	}
	public DatabaseFactory getDatabasefactory() {
		return databasefactory;
	}
	public void setDatabasefactory(DatabaseFactory databasefactory) {
		this.databasefactory = databasefactory;
	}

}
